package cse326.fuellog;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FillupActivity extends Activity
{

	LinkedList <Vehicle> vehicles = MainActivity.getVehicles(MainActivity.db);

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fillup);
		// Not sure if we need this intent.
		// Intent intent = getIntent();

		Spinner vehicleSpinner = (Spinner)findViewById(R.id.vehicles);

		List <String> list = new ArrayList <String>();

		for (Vehicle v: vehicles)
		{
			list.add(v.getColor() + " " + v.getYear() + " " + v.getMake() + " " + v.getModel());
		}
		ArrayAdapter <String> dataAdapter = new ArrayAdapter <String>(this,android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		vehicleSpinner.setAdapter(dataAdapter);

	}

	/**
	 * Validates the fillup form.
	 * 
	 * @param view
	 */
	public void validateFillup(View view)
	{
		EditText gallons = (EditText)findViewById(R.id.gallons);
		EditText fuelPrice = (EditText)findViewById(R.id.fuelPrice);
		EditText mileage = (EditText)findViewById(R.id.mileage);
		Spinner vehicleSpinner = (Spinner)findViewById(R.id.vehicles);
		CheckBox isPartialFill = (CheckBox)findViewById(R.id.isPartialFillup);

		if (gallons.getText().toString().length() == 0)
		{
			Toast.makeText(getApplicationContext(),"Need to enter gallons.",Toast.LENGTH_SHORT).show();
			return;
		}
		if (fuelPrice.getText().toString().length() == 0)
		{
			Toast.makeText(getApplicationContext(),"Need to enter fuel price.",Toast.LENGTH_SHORT).show();
			return;
		}
		if (mileage.getText().toString().length() == 0)
		{
			Toast.makeText(getApplicationContext(),"Need to enter mileage.",Toast.LENGTH_SHORT).show();
			return;
		}

		Fillup f = new Fillup();

		f.setDate(System.currentTimeMillis());
		f.setDB(MainActivity.db);
		f.setGallons(Double.parseDouble(gallons.getText().toString()));
		f.setGasPrice(Double.parseDouble(fuelPrice.getText().toString()));
		f.setMileage(Double.parseDouble(mileage.getText().toString()));
		f.setPartialFill(isPartialFill.isChecked());

		f.setVehicleID(vehicles.get(vehicleSpinner.getSelectedItemPosition()).getDbID());

		f.addToDB();
		Toast.makeText(getApplicationContext(),"Entered successfully.",Toast.LENGTH_LONG).show();

		// Be sure to close the activity when we're done.
		finish();

	}

}
